




//
//  MBTableView.m
//  MBTableView
//
//  Created by MB__Lee on 2019/1/10.
//  Copyright © 2019年 MB__Lee. All rights reserved.
//

#import "MBTableView.h"

#import "MBTableCell.h"

@interface MBTableView()<UITableViewDataSource>

/**
 构造者，在这个类里面进行tableview 的数据设置
 */
@property(nonatomic,strong)MBTableViewBuilder *builder;

@end

static NSString *MBTableCell_identi = @"MBTableCell";

@implementation MBTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style builder:(MBTableViewBuilder *)builder{
    
    MBTableView *mb = [[MBTableView alloc] initWithFrame:frame style:style];
    mb.builder = builder;
    
    mb.dataSource = mb;
    mb.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    mb.rowHeight = 100;
    [mb registerNib:[UINib nibWithNibName:MBTableCell_identi bundle:nil] forCellReuseIdentifier:MBTableCell_identi];
    
    return  mb;
}

#pragma mark --dataSouce @require
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  [self.builder tableView:tableView numberOfRowsInSection:section];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    此处根据数据源来写特定格式的cell 代码
    
    MBTableCell *cell = [tableView dequeueReusableCellWithIdentifier:MBTableCell_identi];
    
    
    
    return  cell;
}

#pragma mark --dataSource @optional
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (![self.builder respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
        return 1;
    }
    
    return  [self.builder numberOfSectionsInTableView:tableView];
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    if (![self.builder respondsToSelector:@selector(tableView:titleForFooterInSection:)]) {
        return nil;
    }
    
    return [self.builder tableView:tableView titleForFooterInSection:section];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (![self.builder respondsToSelector:@selector(tableView:titleForHeaderInSection:)]) {
        return nil;
    }
    
    return [self.builder tableView:tableView titleForHeaderInSection:section];
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if (![self.builder respondsToSelector:@selector(tableView:canEditRowAtIndexPath:)]) {
        return NO;
    }
    
    return  [self.builder tableView:tableView canEditRowAtIndexPath:indexPath];
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    if (![self.builder respondsToSelector:@selector(tableView:canMoveRowAtIndexPath:)]) {
        return nil;
    }
    
    return [self.builder tableView:tableView canMoveRowAtIndexPath:indexPath];
}
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    if (![self.builder respondsToSelector:@selector(sectionIndexTitlesForTableView:)]) {
        return nil;
    }
    
    return  [self.builder sectionIndexTitlesForTableView:tableView];
}
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    if (![self.builder respondsToSelector:@selector(tableView:sectionForSectionIndexTitle:atIndex:)]) {
        return 0;
    }
    
    return [self.builder tableView:tableView sectionForSectionIndexTitle:title atIndex:index];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (![self.builder respondsToSelector:@selector(tableView:commitEditingStyle:forRowAtIndexPath:)]) {
        return;
    }
    
    [self.builder tableView:tableView commitEditingStyle:editingStyle forRowAtIndexPath:indexPath];
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    if (![self.builder respondsToSelector:@selector(tableView:moveRowAtIndexPath:toIndexPath:)]) {
        return;
    }
    [self.builder tableView:tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
}





@end
